<html xmlns="http://www.w3.org/1999/html">
<head>
    <link href="${ctx}/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="${ctx}/font-awesome/3.0.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="${ctx}/css/index.css" rel="stylesheet">
    <link href="${ctx}/bootstrap-table/1.12.0/bootstrap-table.min.css" rel="stylesheet">
    <script src="${ctx}/jquery/2.1.4/jquery.min.js"></script>
    <script src="${ctx}/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="${ctx}/bootstrap-table/1.12.0/bootstrap-table.min.js"></script>
    <link href="${ctx}/wangEditor/3.1.1/wangEditor.min.css" rel="stylesheet">
    <script src="${ctx}/wangEditor/10.0.13/wangEditor.min.js"></script>
    <script src="${ctx}/js/iw.js"></script>
    <link href="${ctx}/bootstrap-switch/css/bootstrap-switch.min.css" rel="stylesheet">
    <script src="${ctx}/bootstrap-switch/js/bootstrap-switch.min.js"></script>
    <script src="${ctx}/layui/layui.all.js"></script>
    <link href="${ctx}/layui/css/layui.css" rel="stylesheet">
    <style>
        [class*='col-xs-'] {
            margin-bottom: 10px;
        }

        .hide {
            display: none;
        }

        .databox {
            margin-bottom: 30px;
        }

        .databox .imgbox {
            width: 200px;
            height: 200px;
            margin: 10px 0;
            position: relative;
        }
    </style>

</head>
<body class="container-fuild" style="padding-top: 8px;">
<form class="form-horizontal" id="form">
    <div class="col-xs-6">
        <div class="form-group">
            <label for="title" class="col-sm-3 control-label pr_no">任务名称</label>
            <div class="col-sm-8">
                <input type="text" style="display:none;" id="taskId" name="taskId"
                       value="<#if task??>${task.taskId!''}</#if>"/>

                <input type="text" class="form-control" id="title" name="title" placeholder="填入应用名称"
                       value="<#if task??>${task.title}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-4 control-label pr_no">承诺审核时间(分)</label>
            <div class="col-sm-8">
                <input type="number" class="form-control" id="auditTime" name="auditTime" placeholder="承诺审核时间单位（分钟）"
                       value="<#if task??>${task.auditTime!0}</#if>">
            </div>
        </div>
    </div>
    <#--  <div class="col-xs-6">
        <div class="form-group">
          <label for="size" class="col-sm-3 control-label">任务总量</label>
          <div class="col-sm-8">
            <input type="number" class="form-control" id="total" name="total" placeholder="填入任务总量，填0表示不设总量" value="<#if task??>${task.total}</#if>">
          </div>
        </div>
      </div>
      <div class="col-xs-6">
        <div class="form-group">
          <label for="size" class="col-sm-3 control-label">每日限量</label>
          <div class="col-sm-8">
            <input type="number" class="form-control" id="amount" name="amount" placeholder="填入每日限量，填0表示不限量" value="<#if task??>${task.amount}</#if>">
          </div>
        </div>
      </div>-->
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label pr_no">客户名称</label>
            <div class="col-sm-8">
                <select id="customer" name="customer"
                        style="height: 34px;width: 253px;border-radius: 4px;border-color: #ccc;">
                    <option value="zy">自营</option>
                    <#if customlist?? && ((customlist?size)>0)>
                        <#list customlist as list>
                            <#if task??>
                                <option value="${list.customId}"
                                        <#if list.customId == task.customer>selected</#if> >${list.name}</option>
                            <#else >
                                <option value="${list.customId}">${list.name}</option>
                            </#if>

                        </#list>
                    </#if>
                </select>
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-4 control-label pr_no">客户单价（单位:分）</label>
            <div class="col-sm-8">
                <input type="number" class="form-control" id="bussAmount" name="bussAmount"
                       placeholder="" value="<#if task??>${task.bussAmount!0}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-4 control-label pr_no">商务结算单价（单位:分）</label>
            <div class="col-sm-8">
                <input type="number" class="form-control" id="increase" name="increase"
                       placeholder="当价格等于0时不可选择“按比例”奖励模式" value="<#if task??>${task.increase}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label pr_no">指定设备</label>
            <div class="col-sm-8">
                <select class="form-control" id="channel" name="channel">
                    <option value="" <#if task?? && task.channel == ''>selected</#if>>不限设备</option>
                    <option value="vivo" <#if task?? && task.channel == 'vivo'>selected</#if>>限vivo</option>
                    <option value="oppo" <#if task?? && task.channel == 'oppo'>selected</#if>>限oppo</option>
                    <option value="huawei" <#if task?? && task.channel == 'huawei'>selected</#if>>限huawei</option>
                    <option value="meizu" <#if task?? && task.channel == 'meizu'>selected</#if>>限meizu</option>
                    <option value="test" <#if task?? && task.channel == 'test'>selected</#if>>限测试版本</option>
                </select>
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-4 control-label pr_no">任务排序</label>
            <div class="col-sm-8">
                <input type="number" class="form-control" id="sort" name="sort" placeholder="升序排序"
                       value="<#if task??>${task.sort}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label pr_no">奖励模式</label>
            <div class="col-sm-8">
                <select class="form-control" id="reward" name="reward">
                    <#list rewards as reward>
                        <option value="${reward}"
                                <#if task?? && task.reward == reward>selected</#if>>${reward.title}</option>
                    </#list>
                </select>
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-4 control-label pr_no">奖励类型</label>
            <div class="col-sm-8">
                <select class="form-control" id="type" name="type">
                    <#list types as type>
                        <option value="${type}" <#if task?? && task.type == type>selected</#if>>${type.title}</option>
                    </#list>
                </select>
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label pr_no">展示奖励</label>
            <div class="col-sm-8">
                <input type="text" class="form-control" id="label" name="label" placeholder="填入展示奖励，如：奖励0.9元"
                       value="<#if task??>${task.label}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-4 control-label pr_no">计算奖励(单位:分)</label>
            <div class="col-sm-8">
                <input type="text" class="form-control" id="worth" name="worth" placeholder="随机奖励格式n:m，其他填n，单位：分"
                       value="<#if task??>${task.worth}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label pr_no">视频指南</label>
            <div class="col-sm-8">
                <input type="text" class="form-control" id="media" name="media" placehotask="填入soso引导指南链接地址"
                       value="<#if task??>${task.media}<#else></#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-4 control-label pr_no">前置条件(包名)</label>
            <div class="col-sm-8">
                <input type="text" class="form-control" id="pkg" name="pkg" placeholder="不填没有前置条件"
                       value="<#if task??>${task.pkg}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label pr_no">任务状态</label>
            <div class="col-sm-8">
                <select class="form-control" id="status" name="status">
                    <option value="Off" <#if task?? && task.status == 'Off'>selected</#if>>下架</option>
                    <option value="On" <#if task?? && task.status == 'On'>selected</#if>>上架</option>

                </select>
            </div>
        </div>
    </div>

    <div class="col-xs-6" style="display: none">
        <div class="form-group">
            <label for="size" class="col-sm-4 control-label pr_no">是否首面显示</label>
            <div class="col-sm-8">
                <input name="showinhp" type="checkbox" <#if task?? && task.showinhp == 1>checked</#if> data-size="mini"
                       value="1"/>
            </div>
        </div>
    </div>

    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-4 control-label pr_no">任务预计用时(分)</label>
            <div class="col-sm-8">
                <input type="number" class="form-control" id="taskTime" name="taskTime" placeholder="预估任务用时单位（分钟）"
                       value="<#if task??>${task.taskTime!0}</#if>">
            </div>
        </div>
    </div>
    <#--<div class="col-xs-6">
        <div class="form-group">
            <label for="icon" class="col-sm-3 control-label pr_no">达到领取上限后需等待(分钟)</label>
            <div class="col-sm-8">
                <input type="text" class="form-control" id="waitTime" name="waitTime" placeholder="达到领取上限后需等待"
                       value="<#if task??>${task.waitTime!0}</#if>">
            </div>
        </div>
    </div>

    <div class="col-xs-6">
        <div class="form-group">
            <label for="icon" class="col-sm-3 control-label pr_no">领取触发隐藏任务的次数</label>
            <div class="col-sm-8">
                <input type="text" class="form-control" id="waitCount" name="waitCount" placeholder="领取触发隐藏任务的次数"
                       value="<#if task??>${task.waitCount!0}</#if>">
            </div>
        </div>
    </div>

    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-4 control-label pr_no">完成后等待时长（单位:小时）</label>
            <div class="col-sm-8">
                <input type="number" class="form-control" id="lessTime" name="lessTime"
                       placeholder="完成后等待时长（单位:小时）" value="<#if task??>${task.lessTime!0}</#if>">
            </div>
        </div>
    </div>-->
    <div class="col-xs-6">
        <div class="form-group">
            <label for="icon" class="col-sm-3 control-label pr_no">任务图标</label>
            <div class="col-sm-8">
                <input type="text" class="form-control" id="icon" name="icon" placeholder="填入图标链接地址"
                       value="<#if task??>${task.icon}</#if>">
            </div>
        </div>
    </div>


    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-4 control-label pr_no">是否可重做</label>
            <div class="col-sm-8" style="padding-top: 5px">
                <input name="redo" type="checkbox" <#if task?? && task.redo == 1>checked</#if> data-size="mini"
                       value="1"/>
            </div>
        </div>
    </div>

    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-5 control-label">可获得奖励倍数</label>
            <div class="col-sm-7">
                <select class="form-control" id="awardMultiple" name="awardMultiple">
                    <option value="1" <#if taskScreenshot?? && taskScreenshot.awardMultiple == 1>selected</#if>>1</option>
                    <option value="2" <#if taskScreenshot?? && taskScreenshot.awardMultiple == 2>selected</#if>>2</option>
                    <option value="3" <#if taskScreenshot?? && taskScreenshot.awardMultiple == 3>selected</#if>>3</option>
                    <option value="4" <#if taskScreenshot?? && taskScreenshot.awardMultiple == 4>selected</#if>>4</option>
                    <option value="5" <#if taskScreenshot?? && taskScreenshot.awardMultiple == 5>selected</#if>>5</option>
                </select>
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-5 control-label">失败可重做次数</label>
            <div class="col-sm-7">
                <select class="form-control" id="failTimes" name="failTimes">
                    <option value="0" <#if taskScreenshot?? && taskScreenshot.failTimes == 0>selected</#if>>0</option>
                    <option value="1" <#if taskScreenshot?? && taskScreenshot.failTimes == 1>selected</#if>>1</option>
                    <option value="2" <#if taskScreenshot?? && taskScreenshot.failTimes == 2>selected</#if>>2</option>
                    <option value="3" <#if taskScreenshot?? && taskScreenshot.failTimes == 3>selected</#if>>3</option>
                    <option value="4" <#if taskScreenshot?? && taskScreenshot.failTimes == 4>selected</#if>>4</option>
                    <option value="5" <#if taskScreenshot?? && taskScreenshot.failTimes == 5>selected</#if>>5</option>
                    <option value="9999" <#if taskScreenshot?? && taskScreenshot.failTimes == 9999>selected</#if>>不限</option>
                </select>
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-5 control-label">任务分类</label>
            <div class="col-sm-7">
                <select class="form-control" id="taskCategoryId" name="taskCategoryId">
                    <#list taskClassifys as taskClassify>
                        <option value="${taskClassify.id}" <#if taskScreenshot?? && taskScreenshot.taskCategoryId??&&taskScreenshot.taskCategoryId == taskClassify.id>selected</#if>>${taskClassify.categoryName}</option>
                    </#list>
                </select>
            </div>
        </div>
    </div>

    <div class="col-xs-12">
        <div class="form-group">
            <label for="size" class="col-sm-5 control-label pr_no">任务标签</label>
            <div class="col-sm-1" style="padding-top: 4px;width:400px;">
                <#list taskTags as taskTag>

                    <input name="taskTagId" type="checkbox" <#if tags?? && tags?seq_contains("${taskTag.id}")>checked</#if> data-size="mini"
                           value="${taskTag.id}"/>${taskTag.tagName}

                </#list>
            </div>

        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="icon" class="col-sm-5 control-label pr_no">任务简介</label>
            <div class="col-sm-7">
                <input type="text" class="form-control" id="taskIntroduce" name="taskIntroduce" placeholder="请输入任务简介"
                       value="<#if taskScreenshot??>${taskScreenshot.taskIntroduce}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="icon" class="col-sm-5 control-label pr_no">任务备注</label>
            <div class="col-sm-7">
                <input type="text" class="form-control" id="taskRemarks" name="taskRemarks" placeholder="请输入任务备注"
                       value="<#if taskScreenshot??>${taskScreenshot.taskRemarks}</#if>">
            </div>
        </div>
    </div>

    <div class="col-xs-12" style="padding-left: 63px">
        <div class="form-group">
            <label for="size" class="control-label">文本描述:</label>
            <#--<div class="col-sm-8"></div>-->
        </div>
    </div>
    <#--<div class="col-xs-12" >
      &lt;#&ndash;<button type="button" id="add_btn2">添加步骤（输入框：输入）</button>
      <button type="button" id="add_btn3">添加步骤（评论内容）</button>
      <input type="text" style="display:none" id="contents" name="contents" />&ndash;&gt;
      <div id="details">
        <div class="databox">
          <input type="text" name="name" placeholder="词名">
          <input type="number" name="amount" placeholder="每日量">
          <input type="number" name="total" placeholder="总量">
          <div class="imgbox">
            <input type="file" onchange="fileReader(this)" accept="image/*" style="position: absolute;width: 100%;height: 100%;opacity: 0;"/>
            <img src="" name="icoUrl" placeholder="图例" width="200px" height="200px">
          </div>
          <input type="text" name="skipurl" placeholder="跳转链接">
          <input type="button" onclick="adddata(this)" style="cursor: pointer" value="添加">
          <input type="button" onclick="addinput(this)" style="cursor: pointer" value="添加任务">
          <input type="checkbox" name="hasplain" placeholder="是否有执行计划">
          执行计划：
          <br>
          <div class="add_input">
          </div>
        </div>
      </div>
      <input type="button" onclick="baoc()" value="保存">
    </div>

    <div class="col-xs-11 col-xs-offset-1" id="_contents">

    </div>-->

    <div id="editor" style="width: 90%;margin-left: 50px;">

    </div>
</form>
<div class="col-xs-12" style="margin-top: 20px;text-align: center">
    <button style="width: 85px;" class="btn btn-success" id="save_btn">保存</button>
</div>
<style scoped>
    .pr_no {
        padding-right: 0;
    }
</style>
<script>
    var E = window.wangEditor
    var editor = new E('#editor')
    $(function () {

        editor.create()

        <#if task??>
        editor.txt.html('${task.contents}')
        </#if>


        $("#save_btn").click(function () {
            if (!$("#title").val()) {
                alert("请填入应用名称！");
                return false;
            }
            if (!$("#icon").val()) {
                alert("请填入应用图标！");
                return false;
            }

            if (!$("#increase").val()) {
                $("#increase").val(0)
            }
            if (!$("#label").val()) {
                alert("请填入展示奖励！");
                return false;
            }
            if (!$("#taskTime").val()) {
                alert("请填入预计完成时间！");
                return false;
            }
            if (!$("#auditTime").val()) {
                alert("请填入承诺审核时间！");
                return false;
            }
            var taskTime = $('#taskTime').val();
            var auditTime = $('#auditTime').val();
            if ($("#reward").val() == 'Random') {
                if (!/:|：/.test($("#worth").val())) {
                    alert("格式为n：m或者n:m，请填入正确的格式！");
                    return false;
                }
            } else {
                if (!/^[1-9]\d*$/.test($("#worth").val())) {
                    alert("请填入计算奖励需正整数！");
                    return false;
                }
            }


            var formData = new window.FormData();

            var taskId = $('#taskId').val();
            var title = $('#title').val();
            var icon = $('#icon').val();
            var total = $('#total').val();
            var amount = $('#amount').val();
            var customer = $('#customer').val();
            var increase = $('#increase').val();
            var channel = $('#channel').val();
            var sort = $('#sort').val();
            var reward = $('#reward').val();
            var type = $('#type').val();
            var label = $('#label').val();
            var worth = $('#worth').val();
            var media = $('#media').val();
            var pkg = $('#pkg').val();
            var status = $('#status').val();
            var contents = editor.txt.html()
            // var lessTime = $('#lessTime').val();
            var bussAmount = $('#bussAmount').val();
            // var waitCount = $('#waitCount').val();
            // var waitTime = $('#waitTime').val();
            var showinhp = 0;
            if ($('[name="showinhp"]').is(":checked")) {
                showinhp = 1;
            }
            var redo = 0;
            if ($('[name="redo"]').is(":checked")) {
                redo = 1;
            }

            var awardMultiple=$("#awardMultiple").val();
            var failTimes=$("#failTimes").val();
            var taskCategoryId=$("#taskCategoryId").val();

            var tagList='';
            $("input[name='taskTagId']:checked").each(function(){
                if(tagList==''){
                    tagList=$(this).val();
                }else{
                    tagList=tagList+','+$(this).val();
                }

            });
            var taskIntroduce=$("#taskIntroduce").val();
            var taskRemarks=$("#taskRemarks").val();

            formData.append("auditTime", auditTime);
            formData.append("taskTime", taskTime);
            formData.append("redo", redo);
            // formData.append("waitCount", waitCount);
            // formData.append("waitTime", waitTime);
            // formData.append("lessTime", lessTime);
            formData.append("bussAmount", bussAmount);
            formData.append("taskId", taskId);
            formData.append("title", title);
            formData.append("icon", icon);
            formData.append("total", 0);
            formData.append("amount", 0);
            formData.append("customer", customer);
            formData.append("increase", increase);
            formData.append("channel", channel);
            formData.append("sort", sort);
            formData.append("reward", reward);
            formData.append("type", type);
            formData.append("label", label);
            formData.append("worth", worth);
            formData.append("media", media);
            formData.append("pkg", pkg);
            formData.append("status", status);
            formData.append("contents", contents);
            formData.append("showinhp", showinhp)
            formData.append("awardMultiple",awardMultiple);
            formData.append("failTimes",failTimes);
            formData.append("taskCategoryId",taskCategoryId);
            formData.append("taskTagIds",tagList);
            formData.append("taskIntroduce",taskIntroduce);
            formData.append("taskRemarks",taskRemarks);

            $.ajax({
                type: "post",
                url: "/multiword/save",
                type: 'POST',
                data: formData,
                async: false,
                cache: false,
                contentType: false, //不设置内容类型
                processData: false, //不处理数据
                success: function (res) {
                    if (res.success) {
                        alert(res.message);
                        window.parent.iw.closeDialog();
                    } else {
                        alert(res.message);
                    }

                }
            });
        });

    });

    function adddata(data) {
        var div = '<div class="databox"><input type="text" name="name" placeholder="词名" >\n' +
            '    <input type="number" name="amount" placeholder="每日量">\n' +
            '    <input type="number" name="total" placeholder="总量">\n' +
            '<div class="imgbox">' +
            '<input type="file" onchange="fileReader(this)" accept="image/*" style="position: absolute;width: 100%;height: 100%;opacity: 0;"/>' +
            '<img src="" name="icoUrl" placeholder="图例" width="200px" height="200px">' +
            '</div>' +
            '    <input type="text" name="skipurl" placeholder="跳转链接">\n' +
            '    <input type="button" onclick="adddata(this)" style="cursor: pointer" value="添加">\n' +
            '    <input type="button" onclick="addinput(this)" style="cursor: pointer" value="添加任务">\n' +
            '<input type="button" onclick="delinput(this)" style="cursor: pointer" value="删除">' +
            '    <input type="checkbox" name="hasplain" placeholder="是否有执行计划">\n' +
            '    执行计划：<div class="add_input">' +
            '    </div>'
        $("#details").append(div);
    }

    function addinput(input) {
        var div = '<p>' +
            '<input type="text" name="date" placeholder="执行日期">' +
            '<input type="number" name="dayamount" placeholder="每日量">' +
            '<input type="button" onclick="delinput(this)" style="cursor: pointer" value="删除">' +
            '</p>'
        $(input).siblings("input[name='hasplain']").prop("checked", true);
        $(input).siblings(".add_input").append(div);
    }

    function delinput(div) {
        var input = div.parentNode.parentNode
        div.parentNode.parentNode.removeChild(div.parentNode);
        if ($(input).children("p").length == 0) {
            $(input).siblings("input[name='hasplain']").prop("checked", false);
        }
    }

    function baoc() {
        var arr = []

        for (var i = 0; i < $('#details').children("div").length; i++) {
            var div = $('#details .databox').eq(i).find(".add_input");
            var input = $('#details .databox').eq(i)[0];
            var list = {}

            list.name = $(input).find('input[name=name]').val();
            list.amount = $(input).find('input[name=amount]').val();
            list.all_total = $(input).find('input[name=total]').val();
            list.skip_url = $(input).find('input[name=skipurl]').val();
            list.ico_url = $(input).find('.imgbox img')[0].src;
            list.has_plan = $(input).find('input[name=hasplain]').is(":checked");
            var arrer = []
            for (var j = 0; j < $(div.prevObject[0]).find("p").length; j++) {
                var list2 = {}
                list2.amount = $(div.prevObject[0]).find("p").eq(j).find("input[name=date]").val()
                list2.run_time = $(div.prevObject[0]).find("p").eq(j).find("input[name=dayamount]").val()
                arrer.push(list2)
                list.MultiwordPlain = arrer
            }
            arr.push(list);
            console.log(arr)
        }
    }

    function fileReader(obj) {
        var file = obj.files;
        var img = $(obj).siblings(".imgbox img")[0];
        var reader = new FileReader();// 操作图片
        if (/image/.test(file[0].type)) {
            reader.readAsDataURL(file[0]);
        } else {
            alert('请选择图片!');
            obj.value = "";
            return;
        }
        // 图片加载错误
        reader.onerror = function () {
            document.write("图片加载错误");
        }
        // 图片加载完成
        reader.onload = function () {
            img.src = reader.result;
        }
    }

    function showmsg(msg) {
        layer.msg(msg)
    }

    function reloadtable() {
        table.bootstrapTable('refresh');
    }
</script>
</body>
</html>
